import { PlayList, Songs } from "@/modules/MusicList";
import { Recommend } from "@/modules/Recommend";
import { getAllPlayList, getLyricWord, getPlayList, getPlayListDetail, getResource } from "@/utils/api";
import { getStoreData } from "@/utils/checkKeyStorage";
import { createSlice } from "@reduxjs/toolkit";

/* 音乐列表的数量，例如创建的歌单的数量 */
const musicList = <PlayList[]>getStoreData('musicList', [])
const resourceList = <Recommend[]>getStoreData('resourceList', [])

const musicListSlice = createSlice({
    name: 'musicListSlice',
    initialState: {
        musicList,
        resourceList,/* 每日推荐 */
        songDetail: {},/* 歌的详细信息 */
        songs: [] as Songs[],/* 列表的 */
        oldSongs: [] as Songs[],/* 进入历史记录 */
        hiddenList: false,/* 隐藏侧边 */
        playList: {} as PlayList,/* 详情 */
        playSongs: {} as any,/* 推荐歌曲 */
        musicLyric: [] as any[],/* 歌词 */
        /* 歌词列表影响 */
        lyricHidden: false
    },
    reducers: {
        saveOldSongs(state, actions) {
            const newState = {
                ...state,
                oldSongs: state.songs
            };
            return newState;
        },
        changeHiddenList(state, action) {
            state.hiddenList = !state.hiddenList
        },
        changeSongDetail(state, action) {
            state.songDetail = action.payload
        },
        chanegeLyricHidden(state, action) {
            state.lyricHidden = !state.lyricHidden
        }
    },
    extraReducers: (builder) => {
        builder.addCase(getPlayList.fulfilled, (state, action) => {
            // console.log(sessionStorage.getItem('playList'))
            if (action.payload) {
                state.musicList = action.payload;
                sessionStorage.setItem('musicList', JSON.stringify(state.musicList));
            } else {
                state.musicList = []
            }
        });
        builder.addCase(getAllPlayList.fulfilled, (state, action) => {
            state.songs = []
            state.songs = action.payload;
        });
        builder.addCase(getResource.fulfilled, (state, action) => {
            if (action.payload) {
                state.resourceList = action.payload
                sessionStorage.setItem('resource', JSON.stringify(state.resourceList));
            } else {
                state.resourceList = []
            }
        });
        builder.addCase(getPlayListDetail.fulfilled, (state, action) => {
            console.log('有人请求了')
            if (action.payload) {
                state.playList = action.payload
            }
        });
        builder.addCase(getLyricWord.fulfilled, (state, action) => {
            console.log('有人请求了')
            if (action.payload) {
                const { payload } = action
                state.musicLyric = []
                var reg = /\[\d{2}:\d{2}\.\d{2,3}\].*/g;
                var songText = payload.match(reg);
                var newWord = []
                console.log('@@', songText)
                if (songText) {
                    for (var i = 0; i < songText.length; i++) {
                        let singleTxt = songText[i].substr(1).split(']');
                        const matches = singleTxt[0].match(/^(\d{2}):(\d{2})\.(\d{2,3})$/);
                        if (matches) {
                            const minute = parseInt(matches[1]);
                            const second = parseInt(matches[2]);
                            const millisecond = parseInt(matches[3]);
                            const timeStamp = minute * 60 * 1000 + second * 1000 + millisecond;
                            newWord.push({ timeStamp, content: singleTxt[1] });
                        }
                    }
                }
                state.musicLyric = newWord
            }
        });
    }
})



export const { saveOldSongs, changeHiddenList, changeSongDetail, chanegeLyricHidden } = musicListSlice.actions

export const { reducer: musicListReducer } = musicListSlice